<?php


namespace App\Shuixing\Controllers;


use App\Admin\Repositories\WxMenu;
use App\Wen\Utils\CircleUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Utils;
use App\Models\Posts\WxComment;
use App\Models\Posts\WxLike;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsImg;
use App\Models\Posts\WxPostsTag;
use App\Models\Posts\WxPostsVideo;
use App\Models\Posts\WxTag;
use App\Models\Posts\WxUserCircle;
use App\Models\Circle\WxCircle;
use App\Models\User\WxUser;
use App\Models\User\WxUserFollow;
use Illuminate\Support\Facades\Cache;

class CircleController
{
    public function index($id, $limit = 5)
    {
        global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
        $__MINI_GLOBAL_IS_ADMIN_SUPER__ = false;
        $circle = WxCircle::where('id', $id)
            ->first();

        if(_empty_($circle)){
            return '<h4 style="text-align: center;margin-top: 100px;">404 无记录</h4>';
        }

        $circle->user_count = WxUserCircle::where('circle_id', $circle->id)->count();
        $circle->posts_count = WxPost::where('circle_id', $circle->id)->count();
        $circle->user = CircleUtils::circleFollowUsers($circle->id);

        $circle_user = WxUser::where('id', $circle->user_id)
            ->first(FieldUtils::userInfoColums());
        if($circle_user){
            $circle_user->like_count = WxLike::where('user_id', $circle->user_id)->count();
            $circle_user->fans_count = WxUserFollow::where('user_follow_id', $circle->user_id)->count();
        }


        $newdata = WxPost::where('circle_id', $id)
            ->where('is_examine', 1)
            ->where('posts_state', 0)
            ->orderBy('id', 'desc')
            ->limit(5)->get();
        $newdata = self::postsParame($newdata);//文章详情

        $data = WxPost::where('circle_id', $id)
            ->where('is_examine', 1)
            ->where('posts_state', 0)
            ->orderBy('is_sticky', 'desc')
            ->orderBy('id', 'desc')
            ->paginate($limit);
        $data = self::postsParame($data);//文章详情
        return view('shuixing/circle', ['data' => $data, 'newdata' => $newdata, 'circle' => $circle, 'circle_user' => $circle_user]);
    }

    public static function postsParame($data)
    {
        $data->map(function ($v) {
            $v->user = WxUser::where('id', $v->user_id)
                ->first(FieldUtils::userInfoColums());//发帖人
            $tagsIds = WxPostsTag::where('posts_id', $v->id)->pluck('tags_id');//话题ID列表
            $v->tags = WxTag::whereIn('id', $tagsIds)->get(['tags_name', 'id']);//话题列表
            $v->like_count = WxLike::where('posts_id', $v->id)->count(); // 笔记喜欢数量
            $v->comment_count = WxComment::where([['posts_id', '=', $v->id], ['comment_state', '=', 1]])->count(); // 评论数量
            $v->image = Utils::imgWithStyle(WxPostsImg::where('posts_id', $v->id)->value('img_url'), 0); // 笔记图片
            if(_empty_($v->image)){
                preg_match_all("/<img.*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", $v->posts_content, $result);
                $images_arr = $result[1];
                if($images_arr){
                    $v->image = Utils::imgWithStyle($images_arr[0], 0);
                }
            }
            $v->vimage = WxPostsVideo::where('posts_id', $v->id)->value('video_thumb_url'); // 笔记视频封面
            $v->format_time = format_datetime($v->created_at); // 发帖时间
            $v->posts_content_raw = preg_replace("/<(img|video).*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", '',$v->posts_content);
            $v->posts_content_raw = trim_all_space(strip_tags(html_entity_decode($v->posts_content_raw)));
        });

        return $data;
    }
}
